package com.itboye.mengwu.app.ui.mymeng;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Observable;
import java.util.Observer;

import org.w3c.dom.Text;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.ImagePagerActivity;
import com.itboye.inageview.ItemEntity;
import com.itboye.inageview.NoScrollGridAdapter;
import com.itboye.inageview.NoScrollGridView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.app.ui.aiwujia.ActivityZengWuXianShouZengFang;
import com.itboye.mengwu.app.ui.aiwujia.ActivityZengWuXianYouKe;
import com.itboye.mengwu.app.ui.aiwujia.ActivityZengWuXianZengSongFang;
import com.itboye.mengwu.app.ui.aiwujia.YuErJingMomDetailActivity;
import com.itboye.mengwu.app.ui.aizhiwu.ItemAdapter;

import com.itboye.mengwu.data.model.BiaoQianModel;
import com.itboye.mengwu.data.model.LuntanTieZiBean;
import com.itboye.mengwu.data.model.MaMaZengShouCangFenYeModel;
import com.itboye.mengwu.data.model.MyTieZiCenterBean;
import com.itboye.mengwu.data.model.QueryCollectionModel;
import com.itboye.mengwu.data.model.QueryCollectionModel.A;
import com.itboye.mengwu.data.presenter.MyTieZiCenterPresenter;
import com.itboye.mengwu.data.presenter.NewMaMaZengPresenter;
import com.itboye.mengwu.data.presenter.TieZiPresenter;
import com.itboye.mengwu.widget.BySqlUtil;
import com.itboye.mengwu.widget.XListView;
import com.itboye.mengwu.widget.XListView.IXListViewListener;

public class CounTxtFragment extends BaseFragment implements Observer,
		IXListViewListener {

	/** Item数据实体集合 */
	private ArrayList<ItemEntity> itemEntities;
	/** ListView对象 */
	private XListView countextListview;
	// private RoundImage image;
	private TextView textGuan;
	private ImageView back;
	// MyTieZiCenterPresenter lunTanPresenter;
	TieZiPresenter tieZiPresenter;
	// MyTieZiCenterBean tieZiCenterBean;
	View UnseView;
	LuntanTieZiBean model;
	RelativeLayout nodata;
	TextView zengwuxian, yuerjing, txtZeng;

	String flag = "1";

	QueryCollectionModel queryCollectionModel, queryCollectionModelTwo;

	// 妈妈赠
	NewMaMaZengPresenter newMaMaZengPresenter;
	String uid;
	MaMaZengShouCangFenYeModel maMaZengShouCangFenYeModel;

	boolean ShangLaflag = false;
	int Page = 1;
	MaMaZengShouCangFenYeModel maMaZengShouCangFenYeModelTwo;

	ItemMaMaZengAdapter itemMaMaZengAdapter = null;
	ItemAdapter itemAdapter = null;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_countext;
	}

	@Override
	public void onMyActivityCreated() {

		maMaZengShouCangFenYeModelTwo = new MaMaZengShouCangFenYeModel();

		countextListview = (XListView) getActivity().findViewById(
				R.id.countextListview);

		countextListview.setXListViewListener(this);
		countextListview.setPullLoadEnable(true);
		countextListview.setPullRefreshEnable(true);
		tieZiPresenter = new TieZiPresenter(this);

		// tieZiPresenter.queryCollection(SPUtils.get(getActivity(), null,
		// SpPublicParams.UID, "") + "", "5", "1");

		newMaMaZengPresenter = new NewMaMaZengPresenter(this);

		// newMaMaZengPresenter.shoucangfenye(uid, page_no, page_size, max_img);
		uid = (String) SPUtils.get(getActivity(), null, SpPublicParams.UID, "");
		showProgressDialog("数据加载中，请稍后", true);
		newMaMaZengPresenter.shoucangfenye(uid, Page + "", "10", "3");

		zengwuxian.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				countextListview.setAdapter(null);
				queryCollectionModel = new QueryCollectionModel();
				queryCollectionModelTwo = new QueryCollectionModel();
				yuerjing.setTextColor(getResources().getColor(R.color.gray));
				zengwuxian.setTextColor(getResources().getColor(
						R.color.main_color));
				txtZeng.setText("赠无限收藏");
				showProgressDialog("数据加载中，请稍后", true);
				Page = 1;
				newMaMaZengPresenter.shoucangfenye(uid, Page + "", "10", "3");
				flag = "1";
				// ByAlert.alert(uid);
			}
		});
		yuerjing.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				countextListview.setAdapter(null);
				queryCollectionModel = new QueryCollectionModel();
				queryCollectionModelTwo = new QueryCollectionModel();
				zengwuxian.setTextColor(getResources().getColor(R.color.gray));
				yuerjing.setTextColor(getResources().getColor(
						R.color.main_color));
				showProgressDialog("数据加载中，请稍后", true);
				txtZeng.setText("育儿经收藏");
				flag = "2";
				Page = 1;
				tieZiPresenter.queryCollection(
						SPUtils.get(getActivity(), null, SpPublicParams.UID, "")
								+ "", "7", Page + "", "10");
				// tieZiPresenter.queryCollection(SPUtils.get(getActivity(),
				// null, SpPublicParams.UID, "") + "", "8", "1");

			}
		});

		countextListview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub

				if ("1".equals(flag)) {
					if (uid.equals(maMaZengShouCangFenYeModel.getList()
							.get(position).getUid())) {
						Intent intent = new Intent(getActivity(),
								ActivityZengWuXianZengSongFang.class);
						startActivity(intent);
						intent.putExtra("uid", uid);
						intent.putExtra("pid", maMaZengShouCangFenYeModel
								.getList().get(position).getPid());
						// ByAlert.alert("跳转了赠送方。~");
					} else if (uid.equals(maMaZengShouCangFenYeModel.getList()
							.get(position).getWinUid())) {
						Intent intent = new Intent(getActivity(),
								ActivityZengWuXianShouZengFang.class);
						intent.putExtra("uid", uid);
						intent.putExtra("pid", maMaZengShouCangFenYeModel
								.getList().get(position).getPid());
						startActivity(intent);
						// ByAlert.alert("跳转了受赠方详情。~");
					} else {
						Intent intent = new Intent(getActivity(),
								ActivityZengWuXianYouKe.class);
						startActivity(intent);
						intent.putExtra("uid", uid);
						intent.putExtra("pid", maMaZengShouCangFenYeModel
								.getList().get(position).getPid());
						// ByAlert.alert("跳转了游客详情。~");
					}

				} else if ("2".equals(flag)) {

					Intent intent2 = new Intent(getActivity(),
							YuErJingMomDetailActivity.class);
					intent2.putExtra("tid",
							queryCollectionModel.getList().get(position - 1)
									.getTid());
					startActivity(intent2);
				}
			}

		});

	}

	/**
	 * 初始化数据
	 */

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			nodata.setVisibility(View.GONE);
			countextListview.setVisibility(View.VISIBLE);
			if (entity.getEventType() == TieZiPresenter.QueryTieziCollectionSuccess) {

				if (ShangLaflag == true) {
					queryCollectionModel = (QueryCollectionModel) entity
							.getData();

					queryCollectionModelTwo.getList().addAll(
							queryCollectionModel.getList());

					itemAdapter.setItemAdapter(getActivity(),
							queryCollectionModelTwo);

					itemAdapter.notifyDataSetChanged();
					if (queryCollectionModelTwo.getList().size() == 0) {
						nodata.setVisibility(View.VISIBLE);
						countextListview.setVisibility(View.GONE);
					} else {
						nodata.setVisibility(View.GONE);
						countextListview.setVisibility(View.VISIBLE);
					}
				} else if (ShangLaflag == false) {

					queryCollectionModel = (QueryCollectionModel) entity
							.getData();
					itemAdapter = new ItemAdapter(getActivity(),
							queryCollectionModel);
					queryCollectionModelTwo = queryCollectionModel;

					countextListview.setAdapter(itemAdapter);

					if (queryCollectionModel.getList().size() == 0) {
						nodata.setVisibility(View.VISIBLE);
						countextListview.setVisibility(View.GONE);
					} else {
						nodata.setVisibility(View.GONE);
						countextListview.setVisibility(View.VISIBLE);
					}
				}
				try {
					closeProgressDialog();
				} catch (Exception e) {

				}
			} else if (entity.getEventType() == TieZiPresenter.QueryTieziCollectionFail) {
				// ByAlert.alert(str);
			}

			if (entity.getEventType() == TieZiPresenter.CollectionTieziSuccess) {
				String str = (String) entity.getData();
				// ByAlert.alert(str);
				tieZiPresenter
						.queryCollection(
								SPUtils.get(getActivity(), null,
										SpPublicParams.UID, "") + "", "7", Page
										+ "", "10");

			} else if (entity.getEventType() == TieZiPresenter.CollectionTieziSuccess) {
				String str = (String) entity.getData();
				// ByAlert.alert(str);
			}

			if (entity.getEventType() == NewMaMaZengPresenter.ShoucangfenyeSuccess) {

				if (ShangLaflag == true) {
					maMaZengShouCangFenYeModel = new MaMaZengShouCangFenYeModel();
					maMaZengShouCangFenYeModel = (MaMaZengShouCangFenYeModel) entity
							.getData();
					maMaZengShouCangFenYeModelTwo.getList().addAll(
							maMaZengShouCangFenYeModel.getList());

					itemMaMaZengAdapter.setItemMaMaZengAdapter(getActivity(),
							maMaZengShouCangFenYeModelTwo);

					itemMaMaZengAdapter.notifyDataSetChanged();
					if (maMaZengShouCangFenYeModelTwo.getList().size() == 0) {
						nodata.setVisibility(View.VISIBLE);
						countextListview.setVisibility(View.GONE);
					} else {
						nodata.setVisibility(View.GONE);
						countextListview.setVisibility(View.VISIBLE);
					}
				} else if (ShangLaflag == false) {

					maMaZengShouCangFenYeModel = new MaMaZengShouCangFenYeModel();
					maMaZengShouCangFenYeModel = (MaMaZengShouCangFenYeModel) entity
							.getData();
					itemMaMaZengAdapter = new ItemMaMaZengAdapter(
							getActivity(), maMaZengShouCangFenYeModel);

					countextListview.setAdapter(itemMaMaZengAdapter);

					maMaZengShouCangFenYeModelTwo = maMaZengShouCangFenYeModel;
					if (maMaZengShouCangFenYeModelTwo.getList().size() == 0) {
						nodata.setVisibility(View.VISIBLE);
						countextListview.setVisibility(View.GONE);
					} else {
						nodata.setVisibility(View.GONE);
						countextListview.setVisibility(View.VISIBLE);
					}
				}
				try {
					closeProgressDialog();
				} catch (Exception e) {

				}
			} else if (entity.getEventType() == NewMaMaZengPresenter.ShoucangfenyeFail) {

			}
			if (entity.getEventType() == NewMaMaZengPresenter.ShoucangSuccess) {
				// ByAlert.alert(entity.getData() + "");

				String str = (String) entity.getData();

				// ByAlert.alert(str);
				newMaMaZengPresenter.shoucangfenye(uid, "", "", "3");

			} else if (entity.getEventType() == NewMaMaZengPresenter.ShoucangFail) {

			}

		} else {
			nodata.setVisibility(View.VISIBLE);
			countextListview.setVisibility(View.GONE);
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {

		}
		stopLoad();
	}

	public class ItemAdapter extends BaseAdapter {

		private Context mContext;
		private QueryCollectionModel queryCollectionModel;

		public ItemAdapter(Context ctx,
				QueryCollectionModel queryCollectionModel) {
			this.mContext = ctx;
			this.queryCollectionModel = queryCollectionModel;

		}

		public void setItemAdapter(Context ctx,
				QueryCollectionModel queryCollectionModel) {
			this.mContext = ctx;
			this.queryCollectionModel = queryCollectionModel;

		}

		@Override
		public int getCount() {
			return queryCollectionModel == null ? 0 : queryCollectionModel
					.getList().size();
		}

		@Override
		public Object getItem(int position) {
			return queryCollectionModel.getList().get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = View.inflate(mContext, R.layout.item_countxt,
						null);
				holder.imgName = (ImageView) convertView
						.findViewById(R.id.imgName);
				holder.txtName = (TextView) convertView
						.findViewById(R.id.txtName);
				holder.cou_content = (TextView) convertView
						.findViewById(R.id.cou_content);
				holder.gridview = (NoScrollGridView) convertView
						.findViewById(R.id.gridview);
				holder.textView2 = (TextView) convertView
						.findViewById(R.id.textView2);
				holder.txtSex = (TextView) convertView
						.findViewById(R.id.txtSex);
				holder.imageView2 = (TextView) convertView
						.findViewById(R.id.imageView2);
				holder.bigline = (LinearLayout) convertView
						.findViewById(R.id.bigline);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			A itemEntity = queryCollectionModel.getList().get(position);

			holder.txtName.setText(itemEntity.getauthor());
			holder.cou_content.setText("[" + itemEntity.getSubject() + " ]   "
					+ itemEntity.getMessage());
			Log.e("hu baby", itemEntity.getBabyDefault()
					+ ",itemEntity.getBabyDefault().getBirthday()="
					+ itemEntity.getBabyDefault().getBirthday());

			if ("".equals(itemEntity.getBabyDefault().getBirthday())
					|| itemEntity.getBabyDefault().getBirthday() == null) {
				holder.textView2.setVisibility(View.GONE);
				holder.txtSex.setVisibility(View.GONE);
			} else {
				if ("1".equals(queryCollectionModel.getList().get(position)
						.getBabyDefault().getSex())) {
					holder.txtSex.setText("男宝");
				} else if ("0".equals(queryCollectionModel.getList()
						.get(position).getBabyDefault().getSex())) {
					holder.txtSex.setText("女宝");
					holder.textView2.setText(babybirth(queryCollectionModel
							.getList().get(position).getBabyDefault()
							.getBirthday()));
				}
			}

			// XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
			// + itemEntity.getHead(), holder.imgName);
			if (itemEntity.getHead() == null || "".equals(itemEntity.getHead())) {
				Log.e("hu2", "sex=" + itemEntity.getSex());
				if ("1".equals(itemEntity.getSex())) {
					// imageView.setImageResource(R.drawable.male);
					holder.imgName.setBackgroundResource(R.drawable.male);
				} else {
					holder.imgName.setImageResource(R.drawable.female);
				}
			} else {
				if (!itemEntity.getHead().startsWith("http")) {
					XImageLoader.load(GlobalConfig.API_URL
							+ "/picture/index?id=" + itemEntity.getHead(),
							holder.imgName);
				} else {
					XImageLoader.load(itemEntity.getHead(), holder.imgName);
				}

			}
			final ArrayList<String> imageUrls = (ArrayList<String>) queryCollectionModel
					.getList().get(position).getImg();
			if (imageUrls == null || imageUrls.size() == 0) { //
				// 没有图片资源就隐藏GridView
				holder.gridview.setVisibility(View.GONE);
			} else {
				holder.gridview.setAdapter(new NoScrollGridAdapter(mContext,
						imageUrls));
			}
			// 点击回帖九宫格，查看大图
			holder.gridview.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					imageBrower(position, imageUrls);
				}
			});
			holder.imageView2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					tieZiPresenter.favorite(
							queryCollectionModel.getList().get(position)
									.getTid(), queryCollectionModel.getList()
									.get(position).getUid(), "0");

				}
			});
			// holder.bigline.setOnClickListener(new OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			//
			// }
			// });

			return convertView;
		}

		/**
		 * 打开图片查看器
		 * 
		 * @param position
		 * @param urls2
		 */
		protected void imageBrower(int position, ArrayList<String> urls2) {
			Intent intent = new Intent(mContext, ImagePagerActivity.class);
			// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
			intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
			intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
			mContext.startActivity(intent);
		}

		/**
		 * listview组件复用，防止“卡顿”
		 * 
		 * @author Administrator
		 * 
		 */
		class ViewHolder {
			private ImageView imgName;
			private TextView txtName, imageView2;
			private TextView cou_content, textView2, txtSex;
			private NoScrollGridView gridview;
			LinearLayout bigline;
		}

		String babybirth(String birth) {
			String babyyears;
			SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyy年MM月dd日");
			Calendar calendar = Calendar.getInstance();
			int yearNow = calendar.get(Calendar.YEAR);
			int monthNow = calendar.get(Calendar.MONTH);
			int dayNow = calendar.get(Calendar.DAY_OF_MONTH);
			Log.d("hu-----", yearNow + "monthNow-----" + "-------dayBirth"
					+ monthNow + "n" + dayNow);
			Calendar calendar2 = Calendar.getInstance();
			long timews = Long.valueOf(birth);
			Date date = new Date(timews * 1000L);
			calendar2.setTime(date);
			// Log.d("hu", date.toString());
			int yearBirth = calendar2.get(Calendar.YEAR);
			int monthBirth = calendar2.get(Calendar.MONTH);
			int dayBirth = calendar2.get(Calendar.DAY_OF_MONTH);
			Log.d("hu-----2", yearBirth + "monthBirth-----" + "-------dayBirth"
					+ monthBirth + "n" + dayBirth);
			int age = yearNow - yearBirth;
			int month = monthNow - monthBirth;
			int day = dayNow - dayBirth;
			if (day < 0) {
				month -= 1;
				calendar2.add(Calendar.MONTH, -1);// 得到上一个月，用来得到上个月的天数。
				day = day + calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
			}
			if (month < 0) {
				month = (month + 12) % 12;
				age--;
			}
			int days = day + 1;
			int months = month + 1;
			if (age == 0) {
				if (month == 0) {
					babyyears = (days + "天");
				} else {
					babyyears = (months + "月" + days + "天");
				}
			} else {
				if (month == 0) {
					babyyears = (age + "岁" + days + "天");
				} else {
					babyyears = (age + "岁" + months + "月" + days + "天");
				}
			}

			return babyyears;
		}

	}

	public class ItemMaMaZengAdapter extends BaseAdapter {

		MaMaZengShouCangFenYeModel maMaZengShouCangFenYeModel;
		Context context;

		public ItemMaMaZengAdapter(Context context,
				MaMaZengShouCangFenYeModel maMaZengShouCangFenYeModel) {
			super();
			this.maMaZengShouCangFenYeModel = maMaZengShouCangFenYeModel;
			this.context = context;
		}

		public void setItemMaMaZengAdapter(Context context,
				MaMaZengShouCangFenYeModel maMaZengShouCangFenYeModel) {

			this.maMaZengShouCangFenYeModel = maMaZengShouCangFenYeModel;
			this.context = context;
		}

		@Override
		public int getCount() {
			return maMaZengShouCangFenYeModel.getList() != null ? maMaZengShouCangFenYeModel
					.getList().size() : 0;
		}

		@Override
		public Object getItem(int position) {

			return maMaZengShouCangFenYeModel.getList() != null ? maMaZengShouCangFenYeModel
					.getList().get(position) : null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = View.inflate(context,
						R.layout.item_countmamazeng, null);
				holder.imgName = (ImageView) convertView
						.findViewById(R.id.imgName);
				holder.txtName = (TextView) convertView
						.findViewById(R.id.txtName);
				holder.cou_content = (TextView) convertView
						.findViewById(R.id.cou_content);
				holder.gridview = (NoScrollGridView) convertView
						.findViewById(R.id.gridview);
				holder.imageView2 = (TextView) convertView
						.findViewById(R.id.imageView2);
				holder.bigline = (LinearLayout) convertView
						.findViewById(R.id.bigline);
				holder.timeTv = (TextView) convertView
						.findViewById(R.id.timeTv);
				holder.biaoqianTv = (TextView) convertView
						.findViewById(R.id.biaoqianTv);
				holder.addressTv = (TextView) convertView
						.findViewById(R.id.addressTv);

				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			MaMaZengShouCangFenYeModel.A itemEntity = maMaZengShouCangFenYeModel
					.getList().get(position);

			holder.txtName.setText(itemEntity.getNickname());
			holder.cou_content.setText(itemEntity.getName());
			holder.biaoqianTv.setText(itemEntity.getCateName());

			BySqlUtil bySqlUtil = new BySqlUtil();
			bySqlUtil.openDataBase(context);
			bySqlUtil.getProviceName(itemEntity.getLocProvince(),
					itemEntity.getLocCity(), context);
			holder.addressTv.setText(bySqlUtil.getProvinceName() + "   "
					+ bySqlUtil.getCityName());

			SimpleDateFormat myFmt1 = new SimpleDateFormat("yyyy-MM-dd");// 年月日的格式
			SimpleDateFormat myFmt2 = new SimpleDateFormat("HH:mm:ss");// 时分秒的格式
			SimpleDateFormat myFmt3 = new SimpleDateFormat("m分钟前");// 最后做时间差用的分钟
			SimpleDateFormat myFmt4 = new SimpleDateFormat("H小时前");// 最后做时间差用的小时

			// 获取当前年月日
			String nowDay = myFmt1.format(System.currentTimeMillis());
			// 获取该position下的帖子年月日
			String positionDay = myFmt1.format(Long.parseLong((itemEntity
					.getCreateTime()) + "") * 1000);
			// 获取该position下的帖子时分秒
			String positionTime = myFmt2.format(Long.parseLong((itemEntity
					.getCreateTime()) + "") * 1000);
			// 如果是当天显示时和分， 如果超过一天显示日期
			if (nowDay.equals(positionDay)) {
				String result;
				Log.e("--------",
						System.currentTimeMillis()
								- Long.parseLong((itemEntity.getCreateTime())
										+ "") * 1000 + "");
				// 3600000是一个小时的时间戳
				// 如果大于一个小时只显示小时，如果小与一个小时显示分钟
				if (3600000 > System.currentTimeMillis()
						- Long.parseLong((itemEntity.getCreateTime()) + "")
						* 1000) {
					result = myFmt3.format(System.currentTimeMillis()
							- Long.parseLong((itemEntity.getCreateTime()) + "")
							* 1000);
					Log.e("--------", "小于一个小时" + result);

					// 如果小于1分钟
					if (60000 > System.currentTimeMillis()
							- Long.parseLong(itemEntity.getCreateTime())) {
						result = "刚刚";
					}

				} else {
					result = myFmt4.format(System.currentTimeMillis()
							- Long.parseLong((itemEntity.getCreateTime()) + "")
							* 1000);
					Log.e("--------", "大于一个小时" + result);
				}
				holder.timeTv.setText(result);
			} else {
				holder.timeTv.setText(positionDay);

			}

			final ArrayList<String> imageUrls = (ArrayList<String>) maMaZengShouCangFenYeModel
					.getList().get(position).getShowImgs();
			if (imageUrls != null) {
				Log.e("111", imageUrls.size() + "");
			}

			if (imageUrls == null || imageUrls.size() == 0) { //
				// 没有图片资源就隐藏GridView
				holder.gridview.setVisibility(View.GONE);
			} else {

				holder.gridview.setAdapter(new NoScrollGridAdapter(context,
						imageUrls));
			}
			// XImageLoader.load(SPUtils.get(mContext, null,
			// SpPublicParams.USERHEAD, "") + "", holder.imgName);

			XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
					+ itemEntity.getHead(), holder.imgName);

			// 点击回帖九宫格，查看大图
			holder.gridview.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					imageBrower(position, imageUrls);
				}
			});

			holder.imageView2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					// tieZiPresenter.favorite(queryCollectionModel.getList().get(position).getTid(),
					// queryCollectionModel.getList().get(position).getUid(),
					// "0");
					// ByAlert.alert("假装收藏了");

				}
			});

			holder.imageView2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					// tieZiPresenter.favorite(queryCollectionModel.getList().get(position).getTid(),
					// queryCollectionModel.getList().get(position).getUid(),
					// "0");
					newMaMaZengPresenter.shoucang(maMaZengShouCangFenYeModel
							.getList().get(position).getPid(), uid, "0", "3");

				}
			});
			holder.bigline.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					if ("1".equals(flag)) {
						if (uid.equals(maMaZengShouCangFenYeModel.getList()
								.get(position).getUid())) {
							Intent intent = new Intent(getActivity(),
									ActivityZengWuXianZengSongFang.class);
							intent.putExtra("uid", uid);
							intent.putExtra("pid", maMaZengShouCangFenYeModel
									.getList().get(position).getPid());
							startActivity(intent);

							// ByAlert.alert("跳转了赠送方。~");
						} else if (uid.equals(maMaZengShouCangFenYeModel
								.getList().get(position).getWinUid())) {
							Intent intent = new Intent(getActivity(),
									ActivityZengWuXianShouZengFang.class);
							intent.putExtra("uid", uid);
							intent.putExtra("pid", maMaZengShouCangFenYeModel
									.getList().get(position).getPid());
							startActivity(intent);
							// ByAlert.alert("跳转了受赠方详情。~");
						} else {
							Intent intent = new Intent(getActivity(),
									ActivityZengWuXianYouKe.class);
							intent.putExtra("uid", uid);
							intent.putExtra("pid", maMaZengShouCangFenYeModel
									.getList().get(position).getPid());
							startActivity(intent);

							// ByAlert.alert("跳转了游客详情。~");
						}
					}

				}
			});

			return convertView;
		}

	}

	class ViewHolder {
		private ImageView imgName;
		private TextView txtName, imageView2, timeTv;
		private TextView cou_content, biaoqianTv, addressTv;
		private NoScrollGridView gridview;
		LinearLayout bigline;
	}

	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(getContext(), ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		getContext().startActivity(intent);
	}

	@Override
	public void onRefresh() {
		ShangLaflag = false;
		Page = 1;
		showProgressDialog("数据加载中，请稍后", true);
		if ("1".equals(flag)) {
			// newMaMaZengPresenter.shoucangfenye(uid, page_no, page_size,
			// max_img);
			newMaMaZengPresenter.shoucangfenye(uid, Page + "", "10", "3");
		} else if ("2".equals(flag)) {
			tieZiPresenter.queryCollection(
					SPUtils.get(getActivity(), null, SpPublicParams.UID, "")
							+ "", "7", Page + "", "10");
		}

	}

	@Override
	public void onLoadMore() {
		ShangLaflag = true;
		Page++;
		showProgressDialog("数据加载中，请稍后", true);
		if ("1".equals(flag)) {
			newMaMaZengPresenter.shoucangfenye(uid, Page + "", "10", "3");
		} else if ("2".equals(flag)) {
			tieZiPresenter.queryCollection(
					SPUtils.get(getActivity(), null, SpPublicParams.UID, "")
							+ "", "7", Page + "", "10");
		}

	}

	public void stopLoad() {
		countextListview.stopLoadMore();
		countextListview.stopRefresh();
	}

}
